package com.ctshk.rpc.user.service.impl;

import com.ctshk.common.dto.Result;
import com.ctshk.common.enums.SystemError;
import com.ctshk.common.exception.BusinessException;
import com.ctshk.common.sms.core.SmsUtil;
import com.ctshk.rpc.user.config.SmsConfig;
import com.ctshk.rpc.user.service.IUserSmsService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Map;

/**
 * <p>
 *
 * </p>
 *
 * @author 李新科
 * @date 2021/6/1 11:07
 */
@DubboService
public class UserSmsServiceImpl implements IUserSmsService {


    @Autowired
    private SmsConfig smsConfig;

    @Override
    public Result sendOrderSms(Integer type, String areaCode, String phone, Map<String, String> smsParam) {
        try {
            boolean domestic = SmsUtil.isDomestic(areaCode);
            if(domestic) {
                SmsUtil.sendSms(areaCode + phone, smsConfig.getQueryNumberTemplateCode(), smsParam);
            } else {
                SmsUtil.sendSms(areaCode + phone, smsConfig.getInternationalQueryNumberTemplateCode(), smsParam);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Result.failed(new BusinessException(SystemError.SYS_433,"短信发送失败"));
        }
        return Result.success();
    }
}
